package com.itheima.wemedia.controller;


import com.itheima.common.vo.PageResultVo;
import com.itheima.common.vo.ResultVo;
import com.itheima.wemedia.dto.WmNewsDtoSave;
import com.itheima.wemedia.dto.WmNewsPageReqDto;
import com.itheima.wemedia.pojo.WmNews;
import com.itheima.wemedia.service.WmNewsService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.RestController;
import com.itheima.core.controller.AbstractCoreController;

import java.util.Map;

/**
 * @description <p>自媒体图文内容信息</p>
 *
 * @version 1.0
 * @package com.itheima.wemedia.controller
 */
@Api(value="WmNewsController",tags = "自媒体图文内容信息")
@RestController
@RequestMapping("/news")
public class WmNewsController extends AbstractCoreController<WmNews> {

    private WmNewsService wmNewsService;

    @Autowired
    public WmNewsController(WmNewsService wmNewsService) {
        super(wmNewsService);
        this.wmNewsService=wmNewsService;
    }

    /**
     * 文章分页查询
     * @param dto
     * @return
     */
    @PostMapping("/list")
    public PageResultVo<WmNews> findPage(@RequestBody WmNewsPageReqDto dto){
        PageResultVo<WmNews> resultVo = wmNewsService.findPage(dto);
        return resultVo;
    }

    /**
     * 发表文章
     * @param draft
     * @param dtoSave
     * @return
     */
    //submit?draft=true
    @PostMapping("/submit")
    public ResultVo<Integer> submit(boolean draft, @RequestBody WmNewsDtoSave dtoSave){
        Integer id = wmNewsService.submit(draft, dtoSave);
        return ResultVo.ok(id);
    }

    /**
     * 通过id查看文章内容
     * @param newsId
     * @return
     */
    @GetMapping("/one/{newsId}")
    public ResultVo<WmNews> findById(@PathVariable(name = "newsId") int newsId){
        WmNews news = wmNewsService.getById(newsId);
        return ResultVo.ok(news);
    }

    /**
     * 通过id删除文章
     * 条件：文章不为发布状态或不为上架状态
     * @param newsId
     * @return
     */
    @GetMapping("/del_news/{newsId}")
    public ResultVo deleteById(@PathVariable (name = "newsId") int newsId){
        wmNewsService.deleteById(newsId);
        return ResultVo.ok();
    }

    /**
     * 文章上架与下架处理
     * @param paramMap
     * @return
     */
    @PostMapping("/down_or_up")
    public ResultVo down_or_up(@RequestBody Map<String,Integer> paramMap){
        wmNewsService.downOrUp(paramMap);
        return ResultVo.ok();
    }

}

